package com.health.db;

import java.util.ArrayList;
import java.util.List;

import com.health.NovaApplication;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.HD_PERSONAL_HEALTH_PHOTO;
import com.health.tools.GlobalConts;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import net.tsz.afinal.FinalDb;

/**
 * 数据库工具类
 *
 * @author 田相伟
 */
public class DbManager {
    public SQLiteDatabase db;
    private FinalDb fdb;
    public static final int pagesize = 9;
    public static final String TYPE_SIGN = "type_sign";
    public static final String TYPE_UNSIGN = "type_unsign";
    public static final String TYPE_ALL = "type_all";

    public DbManager(Context context) {
        SQLdm s = new SQLdm();
        db = s.openDatabase(context);
    }

    // public List<BD_DD_INFO> fenYeFind(int page) {
    // Cursor cursor = db.rawQuery("select * from
    // "+SQLdm.getARCHIVE_ASSESSMENTGUIDE_TableName()+" order by ID limit ? ",
    // new String[]{page*10+""});
    // String name = null;
    // System.out.println("8888888888888888888888");
    // while (cursor.moveToNext()) {
    // name = cursor.getString(cursor.getColumnIndex("IDCARD"));
    // System.out.println(name+"**********************");
    // }
    // cursor.close();
    // return null;
    // }

    // /**
    // * 居民健康档案
    // */
    // //保存居民健康档案
    // public void saveARCHIVE_CARD(ARCHIVE_CARD aRCHIVE_CARD){
    // Object[] arrayOfObject = new Object[11];
    // db.execSQL(, bindArgs);
    // }

    /**
     * 判断照片表是否存在
     */
    public boolean isExistPhoto() {
        boolean flag = false;
        try {
            db.execSQL(
                    " select count(*) from sqlite_master where type = 'table' and name = 'HD_PERSONAL_HEALTH_PHOTO'");
            flag = true;
            Log.w("判断照片表是否存在-----", "存在");
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            Log.w("判断照片表是否存在-----", "不存在");
        }
        return flag;

    }

    /**
     * add persons
     *
     * @param persons
     */
    public int addPhoto(HD_PERSONAL_HEALTH_PHOTO person) {
        int flag = 0;
        db.beginTransaction(); // 开始事务
        try {
            if (!isExistPhoto()) {
                db.execSQL("CREATE TABLE IF NOT EXISTS HD_PERSONAL_HEALTH_PHOTO "
                        + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,CARDID VARCHAR(20),PHYSICALID VARCHAR(30),"
                        + "PERSONALID VARCHAR(30),PHOTO binary,CREATETIME VARCHAR(30),CREATER VARCHAR(30),"
                        + "CREATEORG VARCHAR(30),RES0 VARCHAR(30),RES1 VARCHAR(30),RES2 VARCHAR(30),RES3 VARCHAR(30),"
                        + "RES4 VARCHAR(30),RES5 VARCHAR(30),RES6 VARCHAR(30),RES7 VARCHAR(30),RES8 VARCHAR(30),RES9 VARCHAR(30))");
            }
            db.execSQL(
                    "insert into HD_PERSONAL_HEALTH_PHOTO(CARDID,PHOTO,CREATETIME,CREATEORG,CREATER)values(?,?,?,?,?)",
                    new Object[]{person.getCARDID(), person.getPHOTO(), person.getCREATETIME(), person.getCREATEORG(),
                            person.getCREATER()});
            db.setTransactionSuccessful(); // 设置事务成功完成
            // Log.w("图片插入数据库----成功-----", "tt5555" );
            flag = 1;
        } catch (Exception e) {
            e.printStackTrace();
            flag = -1;
            Log.w("图片插入数据库----异常-----", "tt" + e.getMessage());
        } finally {
            db.endTransaction(); // 结束事务
        }
        return flag;

    }

    /**
     * 更改照片状态
     */
    public int updatePhoto(HD_PERSONAL_HEALTH_PHOTO person) {
        int flag = 1;
        try {
            db.execSQL("update HD_PERSONAL_HEALTH_PHOTO set PHOTO=?,CREATETIME=?,CREATEORG=?,CREATER=? where CARDID=?",
                    new Object[]{person.getPHOTO(), person.getCREATETIME(), person.getCREATEORG(),
                            person.getCREATER(), person.getCARDID()});
        } catch (Exception e) {
            e.printStackTrace();
            flag = -1;
        }
        return flag;
    }

    /**
     * 查询data
     */
    public List<HD_PERSONAL_HEALTH_PHOTO> selectDataPHOTO(String CARDID) {
        List<HD_PERSONAL_HEALTH_PHOTO> list = new ArrayList<HD_PERSONAL_HEALTH_PHOTO>();
        try {
            String strDataPHOTOSql = "select * from HD_PERSONAL_HEALTH_PHOTO  where  CARDID= '" + CARDID + "'";
            Cursor cursor = db.rawQuery(strDataPHOTOSql, null);
            Log.w("sql---插卡图片", strDataPHOTOSql);
            while (cursor.moveToNext()) {
                HD_PERSONAL_HEALTH_PHOTO ques = new HD_PERSONAL_HEALTH_PHOTO();
                ques.setCARDID(cursor.getString(cursor.getColumnIndex("CARDID")));
                ques.setPHOTO(cursor.getBlob(cursor.getColumnIndex("PHOTO")));
                list.add(ques);
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 查询当前管理人数
     */
    public int getCountByJDR(String str_jdrno) {
        int count = 0;
        try {
            String strCountByJDRSql = "select count(ID) from ARCHIVE_BASEINFO  where  CREATER='" + str_jdrno
                    + "' and CREATED_BY='" + str_jdrno + "'";
            Cursor cursor = db.rawQuery(strCountByJDRSql, null);
            Log.w("sql---查询当前人数", strCountByJDRSql);
            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 查询新增人数
     */
    public int getCountByJDRCurrentDate(String str_jdrno, String str_currentDate) {
        int count = 0;
        try {
            String strCountByJDRCurrentDateSql = "select count(ID) from ARCHIVE_BASEINFO  where  CREATER='" + str_jdrno
                    + "' and CREATED_BY='" + str_jdrno + "' and date(CREATED_DATE)=date('" + str_currentDate + "')";

            Cursor cursor = db.rawQuery(strCountByJDRCurrentDateSql, null);
            Log.w("sql---今日新增人数", strCountByJDRCurrentDateSql);
            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 查询类别 1:一般人群，2：儿童，3：孕产妇，4：老年人，5：重精神病，6：高血压，7：糖尿病，8：冠心病，9：脑卒中 10 残疾人默认值1
     */
    public int getCountByJDRType(String str_jdrno, String str_type) {
        int count = 0;
        try {
            String strCountByJDRTypeSql = "select count(ID) from ARCHIVE_BASEINFO  where  CREATER='" + str_jdrno
                    + "' and CREATED_BY='" + str_jdrno + "' and (','||POPULATION_TYPE||',') like '%," + str_type
                    + ",%'";
            Cursor cursor = db.rawQuery(strCountByJDRTypeSql, null);
            Log.w("sql---查询疾病类型种类" + str_type, strCountByJDRTypeSql);
            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 1:一般人群，2：儿童，3：孕产妇，4：老年人，5：重精神病，6：高血压，7：糖尿病，8：冠心病，9：脑卒中 10残疾人 默认值 1 11：肺结核
     * ；12：建档立卡贫困户，13：计划生育特殊家庭 14：卡外贫困户 15：低保户， 默认值1 年龄分类标准：儿童：<=6;孕妇：孕龄标准(14<
     * <=55);老年人：>=65
     */
    public int getCountByJDRJBType(String str_jdrno) {
        int count = 0;
        try {

            String strJDRJBTypeSql = "select count(ID) from ARCHIVE_BASEINFO  where  CREATER='" + str_jdrno
                    + "' and CREATED_BY='" + str_jdrno
                    + "' and  (POPULATION_TYPE in ('5','6','7','8','9','10','11')  )";

            Cursor cursor = db.rawQuery(strJDRJBTypeSql, null);
            Log.w("sql---类型type", strJDRJBTypeSql);
            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 查询当前管理人数
     */
    public int getCountByJDR(String str_jdr, String str_jdrno, String searchContent) {
        String str_where = " AND ( CUSTOMERNAME like '%" + searchContent + "%' or PHONE like '%" + searchContent
                + "%' or IDCARD like '%" + searchContent + "%') ";
        int count = 0;
        try {
            String strJDRSql = "select count(ID) from ARCHIVE_BASEINFO  where  CREATER='" + str_jdr
                    + "' and CREATED_BY='" + str_jdrno + "'" + (TextUtils.isEmpty(searchContent) ? "" : str_where);

            Cursor cursor = db.rawQuery(strJDRSql, null);
            Log.w("sql---创建人", strJDRSql);
            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 查询当前管理人数(查询新页面)
     *
     * @param
     */
    public int getCustomerCount(String str_jdr, String startTime, String endTime, String str_realname, String str_idcard) {
        /*if (TextUtils.isEmpty(startTime) && TextUtils.isEmpty(endTime) && TextUtils.isEmpty(str_realname)&&TextUtils.isEmpty(str_idcard)) {
            return 0;
        }*/
        String str_where = "";
//        String str_where = "  (CUSTOMERNAME like '%" + str_realname + "%'  or IDCARD like '%" + strIdcard + "%' )";

        if (!str_realname.isEmpty()) {
            str_where += "CUSTOMERNAME like '%" + str_realname + "%' and ";
        }
        if (!str_idcard.isEmpty()) {
            str_where += "IDCARD like '%" + str_idcard + "%' and ";
        }
        if (!startTime.isEmpty()) {
            str_where += "created_date >='" + startTime + "' and ";
        }
        if (!endTime.isEmpty()) {
            str_where += "created_date <='" + endTime + "'";
        }
        str_where = str_where.trim();
        if (str_where.endsWith("and")) {
            str_where = str_where.substring(0, str_where.length() - 3);
//            str_where = str_where.replace(str_where.substring(str_where.length()-3, str_where.length()),"");
        }
        int count = 0;
        str_where = TextUtils.isEmpty(str_where) ? str_where : "AND ( " + str_where + ")";
        try {
            String strJDRSql = "select count(ID) from ARCHIVE_BASEINFO" + (TextUtils.isEmpty(str_where) ? "" : "where" + str_where);
/*
            String strJDRSql = "select count(ID) from ARCHIVE_BASEINFO  where  CREATER='" + str_jdr
                    + "' and CREATED_BY='" + str_jdr + "'" + str_where;
*/

            Cursor cursor = db.rawQuery(strJDRSql, null);
            Log.w("sql---创建人", strJDRSql);
            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 查询已签约管理人数
     */
    public int getCountByJDRSign(String str_jdr, String str_jdrno, String searchContent) {
        String str_where = " AND ( CUSTOMERNAME like '%" + searchContent + "%' or PHONE like '%" + searchContent
                + "%' or IDCARD like '%" + searchContent + "%') ";
        int count = 0;
        try {
            String strSignSql = "select count(idcard) from (select a.idcard,a.CREATER,a.PHONE,a.CUSTOMERNAME,b.id from archive_baseinfo a  left OUTER   join JTYS_KHQYB b "
                    + " on a.idcard=b.cardnum) where id is not null" + (TextUtils.isEmpty(searchContent) ? "" : str_where);
           // 一个设备所有居民信息对医生共享
/*
            String strSignSql = "select count(idcard) from (select a.idcard,a.CREATER,a.PHONE,a.CUSTOMERNAME,b.id from archive_baseinfo a  left OUTER   join JTYS_KHQYB b "
                    + " on a.idcard=b.cardnum  where a.CREATER='" + str_jdrno + "' AND a.CREATED_BY='" + str_jdrno
                    + "') where id is not null" + (TextUtils.isEmpty(searchContent) ? "" : str_where);
*/

            // "select count(cardNum) from JTYS_KHQYB a left join
            // ARCHIVE_BASEINFO b on b.IDCARD=a.cardNum where b.CREATER='"
            // + str_jdrno + "' AND b.CREATED_BY='" + str_jdrno + "' order by
            // created_date desc "
            Cursor cursor = db.rawQuery(strSignSql, null);
            Log.w("sql---签约", strSignSql);
            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 查询未签约管理人数
     */
    public int getCountByJDRUnSign(String str_jdr, String str_jdrno, String searchContent) {
        String str_where = " AND ( CUSTOMERNAME like '%" + searchContent + "%' or PHONE like '%" + searchContent
                + "%' or IDCARD like '%" + searchContent + "%') ";
        int count = 0;
        try {
            String strUnSignSql = "select count(idcard) from (select a.idcard,a.CREATER,a.PHONE,a.CUSTOMERNAME,b.id from archive_baseinfo a  left OUTER   join JTYS_KHQYB b "
                    + " on a.idcard=b.cardnum) where id is null" + (TextUtils.isEmpty(searchContent) ? "" : str_where);
            // 一个设备所有居民信息对医生共享
/*
            String strUnSignSql = "select count(idcard) from (select a.idcard,a.CREATER,a.PHONE,a.CUSTOMERNAME,b.id from archive_baseinfo a  left OUTER   join JTYS_KHQYB b "
                    + " on a.idcard=b.cardnum  where a.CREATER='" + str_jdrno + "' AND a.CREATED_BY='" + str_jdrno
                    + "') where id is null" + (TextUtils.isEmpty(searchContent) ? "" : str_where);
*/

            // "select count(ID) from ARCHIVE_BASEINFO a where a.CREATER='" +
            // str_jdrno + "' AND a.CREATED_BY='"
            // + str_jdrno+ "' and not EXISTS (select cardNum from JTYS_KHQYB
            // where a.IDCARD= cardNum) order by created_date desc",

            Cursor cursor = db.rawQuery(strUnSignSql, null);
            Log.w("sql---未签约", strUnSignSql);

            cursor.moveToFirst();
            long cout_long = cursor.getLong(0);
            count = (int) cout_long;
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
            count = 0;
        }
        return count;

    }

    /**
     * 获取用户状态
     *
     * @return
     * @author 王婷婷
     * @date 2017年6月7日 上午10:41:36
     */
    public List<ARCHIVE_BASEINFO> selectUserByQYSignPage(String startTime, String endTime, String str_jdrno, String str_realname, int currentpage, String type,
                                                         String strIdcard) {
        /*if (TextUtils.isEmpty(startTime) && TextUtils.isEmpty(endTime) && TextUtils.isEmpty(str_realname)&&TextUtils.isEmpty(strIdcard)) {
            return null;
        }*/
        String str_where = "";
//        String str_where = "  (CUSTOMERNAME like '%" + str_realname + "%'  or IDCARD like '%" + strIdcard + "%' )";

        if (!str_realname.isEmpty()) {
            str_where += "CUSTOMERNAME like '%" + str_realname + "%' and ";
        }
        if (!strIdcard.isEmpty()) {
            str_where += "IDCARD like '%" + strIdcard + "%' and ";
        }
        if (!startTime.isEmpty()) {
            str_where += "created_date >='" + startTime + "' and ";
        }
        if (!endTime.isEmpty()) {
            str_where += "created_date <='" + endTime + "'";
        }
        str_where = str_where.trim();
        if (str_where.endsWith("and")) {
            str_where = str_where.substring(0, str_where.length() - 3);
//            str_where = str_where.replace(str_where.substring(str_where.length()-3, str_where.length()),"");
        }
        List<ARCHIVE_BASEINFO> list = new ArrayList<ARCHIVE_BASEINFO>();
        try {
            int begin = (currentpage - 1) * pagesize;
            // 已签约sql
            // String str_sqlSign = "select * from JTYS_KHQYB a left join
            // ARCHIVE_BASEINFO b on b.IDCARD=a.cardNum where b.CREATER='"
            // + str_jdrno + "' AND b.CREATED_BY='" + str_jdrno + "' order by
            // created_date desc limit " + begin
            // + "," + pagesize;

            // 未签约sql
            // String str_sqlUnSign = "select * from ARCHIVE_BASEINFO a where
            // a.CREATER='" + str_jdrno
            // + "' AND a.CREATED_BY='" + str_jdrno
            // + "' and not EXISTS (select cardNum from JTYS_KHQYB where
            // a.IDCARD= cardNum) order by created_date desc limit "
            // + begin + "," + pagesize;
            if (TextUtils.isEmpty(type)) {
                list = new ArrayList<ARCHIVE_BASEINFO>();
            } else {
                String idContent = "";

                if (TYPE_SIGN.equals(type)) {
                    idContent = " where  id is not null "
                            + (TextUtils.isEmpty(str_realname) ? "" : " and (" + str_where + ")");
                } else if (TYPE_UNSIGN.equals(type)) {
                    idContent = " where id is null "
                            + (TextUtils.isEmpty(str_realname) ? "" : " and (" + str_where + ")");
                } else if (TYPE_ALL.equals(type)) {
                    idContent = TextUtils.isEmpty(str_where) ? "" : " where " + str_where;
//                    idContent = "" + (TextUtils.isEmpty(searchContent) ? "" : " where " + str_where);
                }

                String str_sqlSignByQYSignPage = "select CUSTOMERNAME,BIRTHDAY,SEX,PHONE,POPULATION_TYPE,HOUSEHOLD_ADDR,IDCARD,created_date,id,IS_UPDATE " +
                        " from (select a.idcard,a.CREATER,a.CUSTOMERNAME,a.BIRTHDAY,a.SEX,a.PHONE,a.POPULATION_TYPE,a.HOUSEHOLD_ADDR,a.created_date,a.IS_UPDATE,b.id " +
                        "from archive_baseinfo a " +
                        " left OUTER   join JTYS_KHQYB b " +
                        " on a.idcard=b.cardnum)" + idContent + " order by created_date desc limit " + begin + "," + pagesize;

                //同一设备的居民所有医生共享，修改之：
                /*String str_sqlSignByQYSignPage = "select CUSTOMERNAME,BIRTHDAY,SEX,PHONE,POPULATION_TYPE,HOUSEHOLD_ADDR,IDCARD,created_date,id,IS_UPDATE " +
                        " from (select a.idcard,a.CREATER,a.CUSTOMERNAME,a.BIRTHDAY,a.SEX,a.PHONE,a.POPULATION_TYPE,a.HOUSEHOLD_ADDR,a.created_date,a.IS_UPDATE,b.id " +
                        "from archive_baseinfo a " +
                        " left OUTER   join JTYS_KHQYB b " +
                        " on a.idcard=b.cardnum  " +
                        "where a.CREATER='" + str_jdrno +
                        "' AND a.CREATED_BY='" + str_jdrno
                        + "') " + idContent + " order by created_date desc limit " + begin + "," + pagesize;
*/

                Cursor cursor = db.rawQuery(str_sqlSignByQYSignPage, null);
                Log.w("sql---查找机构--type" + "\n" + idContent, str_sqlSignByQYSignPage);

                while (cursor.moveToNext()) {
                    ARCHIVE_BASEINFO info = new ARCHIVE_BASEINFO();
                    info.setCUSTOMERNAME(cursor.getString(cursor.getColumnIndex("CUSTOMERNAME")));
                    info.setBIRTHDAY(
                            GlobalConts.datetimeFormat.parse(cursor.getString(cursor.getColumnIndex("BIRTHDAY"))));
                    info.setSEX(cursor.getString(cursor.getColumnIndex("SEX")));
                    info.setPHONE(cursor.getString(cursor.getColumnIndex("PHONE")));
                    info.setPOPULATION_TYPE(cursor.getString(cursor.getColumnIndex("POPULATION_TYPE")));
                    info.setHOUSEHOLD_ADDR(cursor.getString(cursor.getColumnIndex("HOUSEHOLD_ADDR")));
                    info.setIDCARD(cursor.getString(cursor.getColumnIndex("IDCARD")));
                    // 作为签约状态
                    info.setDISTRICT_ID(cursor.getString(cursor.getColumnIndex("id")));
                    info.setIS_UPDATE(cursor.getString(cursor.getColumnIndex("IS_UPDATE")));

                    list.add(info);
                }
                cursor.close();
            }

        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 执行删除操作
     *
     * @param table
     * @param whereClause
     * @param whereArgs
     * @author 王婷婷
     * @date 2017年12月18日 下午3:53:39 String args[] ={ today, content,
     * Boolean.toString(checked)}; dbServices.delete("_today_plan",
     * "[_Date]=? and [Item]=? and [Check]=?",args);
     */
    public void delete(String table, String whereClause, String[] whereArgs) {
        db.delete(table, whereClause, whereArgs);
        Log.d("Delete", whereClause);
    }

    /**
     * 执行删除操作 String sql = "delete from " + bm + " where cardNum='" + cardNum +
     * "';";
     *
     * @author 王婷婷
     * @date 2017年12月18日 下午3:53:39
     */
    public void deleteBySQL(String sqlDeleteData) {
        try {
            Log.e("deleteBySQL", sqlDeleteData);
            db.execSQL(sqlDeleteData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param db
     * @param tableName  表名
     * @param columnName 列名
     * @return
     */
    public boolean checkColumnExists(String tableName, String columnName) {
        boolean result = false;
        Cursor cursor = null;

        try {
            cursor = db.rawQuery("select * from sqlite_master where name = ? and sql like ?",
                    new String[]{tableName, "%" + columnName + "%"});
            result = null != cursor && cursor.moveToFirst();
        } catch (Exception e) {
            Log.e("tag", "checkColumnExists2..." + e.getMessage());
        } finally {
            if (null != cursor && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return result;
    }

    /**
     * @param sqlDeleteData
     */
    public void addColumnByBySQL(String tableName, String columnName) {
        try {
            boolean checkColumnExists = NovaApplication.dbManger.checkColumnExists(tableName, columnName);
            if (!checkColumnExists) {
                String addColumnSQL = "alter table " + tableName + " add column " + columnName + " varchar";
                db.execSQL(addColumnSQL);
                Log.d("addColumnByBySQL", addColumnSQL);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加索引
     */
    public void addIndexOnTable() {
        try {
            List<String> sqlStrs = new ArrayList<>();

            // 公卫 ARCHIVE_BASEINFO 添加索引IDCARD，(CREATER,
            // CREATED_BY,POPULATION_TYPE)
            sqlStrs.add(addIndexBySQL("index_baseinfo_idcard", "ARCHIVE_BASEINFO", "IDCARD"));
            sqlStrs.add(
                    addIndexBySQL("index_baseinfo_creater", "ARCHIVE_BASEINFO", "CREATER, CREATED_BY,POPULATION_TYPE"));
            // 家庭医生 JTYS_KHQYB 添加索引cardNum
            sqlStrs.add(addIndexBySQL("index_khqyb_creater", "JTYS_KHQYB", "cardNum"));
            // 家庭医生 JTYS_FWMYD 添加索引(creater,satisfiedState)
            sqlStrs.add(addIndexBySQL("index_fwmyd_creater", "JTYS_FWMYD", "creater,satisfiedState"));

            for (int i = 0, size = sqlStrs.size(); i < size; i++) {
                db.execSQL(sqlStrs.get(i));
                Log.e("addIndexOnTable", sqlStrs.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过sql创建索引
     *
     * @param tableName
     * @param columnName
     * @author 王婷婷
     * @date 2018年1月12日 下午3:51:01
     */
    private String addIndexBySQL(String indexName, String tableName, String columnNameStr) {
        String sqlStr = "";
        sqlStr = " create index if not exists " + indexName + " on " + tableName + "(" + columnNameStr + ")";
        return sqlStr;
    }

}
